<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\space\model;

use app\cassification\model\ClassificationDictionary;
use app\project\model\project;
use plugin\saiadmin\basic\BaseModel;

/**
 * 空间列表模型
 */
class space extends BaseModel
{

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'space_id';

    /**
     * 数据库表名称
     * @var string
     */
    protected $table = 'eb_space';
    protected $append = [
        'rent_nature_text',
        'space_class_text',
        'project_id_text',
    ];
    
    /**
     * 空间标题 搜索
     */
    public function searchSpaceTitleAttr($query, $value)
    {
        $query->where('space_title', 'like', '%'.$value.'%');
    }

    public function getrentNatureTextAttr($value,$data){
       
        if(isset($data['rent_nature'])){
            return    ClassificationDictionary::where('dictionary_id', $data['rent_nature'])->value('classification_name_zh');
        }
        return '';
    }
    public function getSpaceClassTextAttr($value,$data){
       
        if(isset($data['space_class'])){
            return    ClassificationDictionary::where('dictionary_id', $data['space_class'])->value('classification_name_zh');
        }
        return '';
    }
    public function getProjectIdTextAttr($value,$data){
        if(isset($data['project_id'])){
            return  project::where('project_id', $data['project_id'])->value('project_title');
        }
        return '';
    }
        /**
     * 空间多图保存数组转换
     */
    public function setSpaceImagesAttr($value)
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE);
    }

     /**
     * 空间多图读取数组转换
     */
    public function getSpaceImagesAttr($value)
    {
        return json_decode($value ?? '', true);
    }
    

}
